﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OracleClient;
using TimeTableEasy.src.Database;
using TimeTableEasy.src.Core;
using System.Collections;

namespace TimeTableEasy
{
    public partial class Cursus_Choose : Form
    {
        private ArrayList _IdList = new ArrayList();
        private int _CampusId = 0;
        private string _Cursus = "";
        private OpCodes.TimeTableEasyAction _Action;

        public Cursus_Choose(OpCodes.TimeTableEasyAction action)
        {
            InitializeComponent();

            this._Action = action;

            cursusList.Items.Clear();
            campusList.Items.Clear();

            if (this._Action == OpCodes.TimeTableEasyAction.ACTION_DEL)
                OK.Text = "Supprimer";
            else if (this._Action == OpCodes.TimeTableEasyAction.ACTION_MOD)
                OK.Text = "Modifier";

            OracleDataReader array = OracleConnector.Query("SELECT CAM_NUM, CAM_NOM FROM CAMPUS ORDER BY (CAM_NOM)");

            if (array == null) return;

            while (array.Read())
            {
                _IdList.Add(array.GetDecimal(0));
                campusList.Items.Add(array.GetString(1));
            }
        }

        private void OK_Click(object sender, EventArgs e)
        {
            if (this._Action == OpCodes.TimeTableEasyAction.ACTION_MOD)
            {

                if (_Cursus != cursusList.Text)
                {
                    OracleDataReader arraycursus = OracleConnector.Query("UPDATE CURSUS SET Cur_NomDate = '" + cursusList.Text + "' WHERE Cur_NomDate = '" + _Cursus + "'");
                    OracleDataReader arraycursus0 = OracleConnector.Query("UPDATE CAMPUS_CURSUS SET CC_CursusNum = '" + cursusList.Text + "' WHERE CC_CampusNum = " + _CampusId + "  and CC_CursusNum = '" + _Cursus + "'");
                    OracleDataReader arraycursus1 = OracleConnector.Query("UPDATE ETUDIANT_CURSUS SET EC_CursusNom = '" + cursusList.Text + "' WHERE EC_EtudiantNum IN (select Ecp_EtudiantNum from ETUDIANT_CAMPUS_PERIODE where Ecp_CampusNum = " + _CampusId + " and Ecp_PeriodeNum = (select Per_Num from PERIODE where (TO_DATE(Sysdate, 'dd/mm/rrrr') >= Per_DateDebut and  TO_DATE(Sysdate, 'dd/mm/rrrr') <= Per_DateFin)))");

                    if ((arraycursus == null) || (arraycursus1 == null) || (arraycursus0 == null))
                        Label_Error.Text = Lang.GetMessage(OpCodes.TimeTableEasyStrings.LANG_ADMIN_CAMPUS_MOD_ERROR);
                    else
                        Label_Error.Text = Lang.GetMessage(OpCodes.TimeTableEasyStrings.LANG_ADMIN_CAMPUS_MOD);
                }
                else
                    Label_Error.Text = Lang.GetMessage(OpCodes.TimeTableEasyStrings.LANG_ALL_FIELD_NULL);

            }
            else if (this._Action == OpCodes.TimeTableEasyAction.ACTION_DEL)
                return;
        }

        private void campusList_SelectedIndexChanged(object sender, EventArgs e)
        {
            this._CampusId = int.Parse(this._IdList[campusList.SelectedIndex].ToString());

            OracleDataReader array= OracleConnector.Query("SELECT CC_CURSUSNUM FROM CAMPUS_CURSUS WHERE CC_CAMPUSNUM = " + _CampusId);

            if (array == null) return;

            while (array.Read())
                cursusList.Items.Add(array.GetString(0));
        }

        private void cursusList_SelectedIndexChanged(object sender, EventArgs e)
        {
            _Cursus = cursusList.SelectedItem.ToString();
        }
    }
}
